<!-- file dokumen -->
<script> 
	
	$(function(){
		/* REMOVE DUPLICATE ID */
		var ids = [];
		$('*').each(function() {
			if (this.id && this.id !== '') {
				if (ids[this.id]) {
					$(this).remove();
				} else {
					ids[this.id] = this
				}
			}
		});
		
		/* external function */
		
		$.fn.fmatter.downloadBtn = function(cellval, opts) {
			return "<a href='<?php echo base_url()."../"; ?>"+cellval+"' class='downloadBtn' target='_blank'>Download</a>";
		};
		
		$.fn.fmatter.keaslianFormat = function(cellval, opts) {
			switch (cellval){
				<?php foreach($data_dokumen_asli as $k=>$value )
					echo " case '".$k."' : return '".$value."' ; break;";
				?>
				default : return cellval;
			}
		};
		 arr_dokumen = [];
		<?php foreach($data_dokumen_asli as $k=>$value )
					echo " arr_dokumen['".$k."'] = '".$value."'; ";
				?>
		$.fn.serializeObject = function()
		{
		   var o = {};
		   var a = this.serializeArray();
		   $.each(a, function() {
			   if (o[this.name]) {
				   if (!o[this.name].push) {
					   o[this.name] = [o[this.name]];
				   }
				   o[this.name].push(this.value || '');
			   } else {
				   o[this.name] = this.value || '';
			   }
		   });
		   return o;
		};
		
		/* declarisasi */
		
		task_dokumen = $("#task_dokumen");
		grid_dokumen = $("#grid-dokumen");
		pgrid_dokumen = $("#pgrid-dokumen");
		btnadd_dokumen = $("#btn-add-dokumen");
		btnedit_dokumen = $("#btn-edit-dokumen");
		btndelete_dokumen = $("#btn-delete-dokumen");
		btncancel_dokumen =  $("#btn-cancel-dokumen");
		btnsave_dokumen = $("#btn-save-dokumen");
		form_dokumen = $("#form-dokumen");
		
		field_dokumen_id = $("#dokumen_id");
		field_dokumen_nama = $("#dokumen_nama");
		field_dokumen_url = $("#dokumen_url");
		field_dokumen_asli = $("#dokumen_asli");
		field_dokumen_posisi = $("#dokumen_posisi");
		field_t_proyek_id = $("#t_proyek_id");
		
		/* method started */
		
		$( "input[type=submit],input[type=button],input[type=reset], button" ).button();
			
		/* make component */
		
		grid_dokumen.jqGrid({ 
			ajaxGridOptions : {type:"POST"},
			serializeGridData : function(postdata) {return postdata; },
			postData : {task : 'LIST'},
            url: 'c_dokumen',
			datatype: "json", 
			colNames:[
				'Nama Dokumen',
				'Keaslian',
				'Posisi',
				'Nama Proyek',
				'','','dokumen_id'],
			colModel:[
				{ name: 'dokumen_nama'  ,index: 'dokumen_nama' , search : true},
				{ name: 'dokumen_asli'  ,index: 'dokumen_asli' , search : true, formatter : 'keaslianFormat'},
				{ name: 'dokumen_posisi'  ,index: 'dokumen_posisi' , search : true},
				{ name: 'proyek_nama'  ,index: 'proyek_nama' , search : true},
				{ name: 'dokumen_url'  ,index: 'dokumen_url',  width: '75', search : false, formatter : 'downloadBtn'},
				{ name: 't_proyek_id'  ,index: 't_proyek_id' , search : true, hidden: true },
				{ name: 'dokumen_id'  ,index: 'dokumen_id',key: true, hidden: true , search : false}
			],
			width: '900',
			shrinkToFit: false,
			height: 'auto',
			rowNum:10, 
			rowList:[10,20,30], 
			pager: '#pgrid-dokumen', 
			sortname: 'dokumen_id', 
			viewrecords: true, 
			sortorder: "desc",
			caption:"" 
		}); 
		grid_dokumen.jqGrid('navGrid','#pgrid-dokumen',{edit:false,add:false,del:false,search:false});
		grid_dokumen.jqGrid('filterToolbar',{});
		
		form_dokumen.dialog({
			autoOpen: false,
			height: "auto",
			width: "auto",
			modal: true
		});
		
		/* event */

		btnedit_dokumen.click(function(){
		
			id = grid_dokumen.jqGrid("getGridParam","selrow");
			if (id != null){
				task_dokumen.val("UPDATE");
				var data = $(this).data();
				dataRow = grid_dokumen.getRowData(id);
				field_dokumen_id.val(dataRow.dokumen_id);
				field_dokumen_nama.val(dataRow.dokumen_nama);
				field_dokumen_url.val(dataRow.dokumen_url);
				field_dokumen_asli.val(arr_dokumen.indexOf(dataRow.dokumen_asli));
				field_dokumen_posisi.val(dataRow.dokumen_posisi);
				field_t_proyek_id.val(dataRow.t_proyek_id);
				form_dokumen.dialog("open");
			}
		});
		
		btndelete_dokumen.click(function(){
			id = grid_dokumen.jqGrid("getGridParam","selrow");
			if (id != null){
				confr = confirm("Apa Anda yakin menghapus data ini?");
				if (confr){
					var data = $(this).data();
					$.ajax({
						type: 'POST',
						url: 'c_dokumen',
						data: {dokumen_id: id, task : 'DELETE'},
						success: function(response){
							if(response==1){
								grid_dokumen.trigger('reloadGrid');
								$("#grid-dokumen").trigger('reloadGrid');
							}
						}
					});
				}
			}
		});
		btnadd_dokumen.click(function(){
			task_dokumen.val("INSERT");
			form_dokumen.dialog("open");
		});
		
		btncancel_dokumen.click(function(){
			form_dokumen.dialog("close");
		});
		
		btnsave_dokumen.click(function(){
			formupload = document.getElementById(form_dokumen.attr("id"));
			var formData = new FormData(formupload);
			var xhr = new XMLHttpRequest();
			xhr.open('POST', "c_dokumen", true);
			xhr.send(formData);
			xhr.onreadystatechange=function(){
				if (xhr.readyState==4){
					grid_dokumen.trigger('reloadGrid');
					btncancel_dokumen.click();
				}
			}
			
			// data = form_dokumen.serializeObject();
			// $.ajax({
				// url : "c_dokumen",
				// data : data,
				// type : 'POST'
			// }).done(function(response){
				// grid_dokumen.trigger('reloadGrid');
				// btncancel_dokumen.click();
			// });
		});
		
	});
</script>

	<table id="grid-dokumen" class="table table-striped table-bordered" ></table>
	<div id="pgrid-dokumen"></div>
	
	<button id="btn-add-dokumen">Tambah</button>
	<button id="btn-edit-dokumen">Ubah</button>
	<button id="btn-delete-dokumen">Hapus</button>
	
	<form id="form-dokumen" title="dokumen" disabled=true >
		<input type="hidden" value="INSERT" name="task" id="task_dokumen">
	   <table>
			<tr style="display: none">
				<td align="right">dokumen_id:</td>
				<td align="left"><input id="dokumen_id" name="dokumen_id" /></td>
			</tr>
			
			<tr > <td align="right">Nama Proyek:</td> <td align="left">
			<?php echo form_dropdown("t_proyek_id" ,$data_t_proyek_id,"",'id="t_proyek_id" class="autocomplete"'); ?></td> </tr>
			<tr > <td align="right">Nama Dokumen:</td> <td align="left"><input id="dokumen_nama" name="dokumen_nama" /></td> </tr>
			<tr > <td align="right">Keaslian Dokumen:</td> <td align="left">
			<?php echo form_dropdown("dokumen_asli" ,$data_dokumen_asli,"",'id="dokumen_asli" class="autocomplete"'); ?></td> </tr>
			<tr > <td align="right">Posisi:</td> <td align="left"><input id="dokumen_posisi" name="dokumen_posisi" /></td> </tr>
			<tr > <td align="right">File:</td> <td align="left"><input id="userfile" name="userfile" type="file"/></td> </tr>
			
			<tr>
				<td align="right"></td>
				<td style="padding-top: 10px;" align="right">
					<input style="margin-right: 5px;" type="button" id="btn-save-dokumen" value="Upload"  />
					<input id="btn-cancel-dokumen" type="reset" value="Cancel" /></td>
			</tr>
		</table>
	</form>